{ // 使用自定义类的原型
    let obj = {
        x: 'x',
        y: 'y'
    }

    class Cls {
        x = 1
        y = 2
        play(){
            console.log(`Hi，${this.x}!`)
        }
    }

    console.log(Cls.prototype.x)  // => undefined
    console.log(obj.x)  // => x
    Cls.prototype.play(obj)  // => Hi，undefined!
    Cls.prototype.play.call(obj)  // => Hi，x!

    console.log(obj.z)  // => undefined
    console.log(obj.prototype)  // => undefined
    console.log(obj.prototype.x)  // => TypeError: Cannot read properties of undefined (reading 'x')
}

{ // 使用 Array 的原型
    let a = {"0": "a", "1": "b", "2": "c", length: 3}; // An array-like object
    let b = [
        Array.prototype.join.call(a, "+"),  // => "a+b+c"
        Array.prototype.map.call(a, x => x.toUpperCase()), // => ["A","B","C"]
        Array.prototype.slice.call(a, 0), // => ["a","b","c"]: true array copy
        Array.from(a), // => ["a","b","c"]: easier array copy
    ]
}

{ // Prototypal Inheritance (原型继承)
    function Animal(name) {
        this.name = name
    }

    Animal.prototype.eat = function () {
        return `${this.name} eats.`
    }

    Animal.prototype.run = function () {
        return `${this.name} is running!!`
    }

    function Dog(name) {
        Animal.call(this, name)
    }

    Dog.prototype = Object.create(Animal.prototype)
    Dog.prototype.constructor = Dog

    Dog.prototype.eat = function () {
        return `${this.name} can't eat!`
    }

    let coco = new Dog('Coco')

    console.log(coco.eat())
    console.log(coco.run())
}